"""
jackson.utils

Abstract functions used by more than one app.
"""
import hashlib
from time import time

def key_in_range(key, low, high):
    """ Returns True if key falls between low and high in the keyspace, false otherwise.
    @author: ep
    @param key: key whose location is in question
    @param low: beginning of the range to check
    @param high: end of the range to check
    @note: the range checked is half-open on the left, ie it is (low, high]
    @note: the range (a,a] is interpreted to be the whole keyspace
    """
    assert len(key) == len(low)
    assert len(low) == len(high)

    if low == high:
        return True
    
    # Respect the circular shape of the keyspace
    if low < high:
        return (low < key <= high)
    else:
        return not (high < key <= low)


def gen_hash(s):
    """
    Given a string, returns the MD5 hash of that string
    @param s: a string
    @rtype: a string that represents a 128-bit hexadecimal number
    """
    m = hashlib.md5()
    m.update(s)
    return m.hexdigest()

def timestamp():
    """ useful for leave messages and RTTs
    """
    return time()